package com.swy.shopgroup.controller;

import com.swy.shopgroup.ResultUtils;
import com.swy.shopgroup.UploadFilePathConfig;
import com.swy.shopgroup.dao.ShopInfoMapper;
import com.swy.shopgroup.dao.ShopUserMapper;
import com.swy.shopgroup.entity.Result;
import com.swy.shopgroup.entity.ShopInfo;
import com.swy.shopgroup.entity.ShopUser;
import com.swy.shopgroup.service.ShopService;
import com.swy.shopgroup.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

@Controller
@RequestMapping("/Shop")
public class ShopController {

    @Resource
    private ShopService shopService;
    @Resource
    private ShopInfoMapper infoMapper;

    @RequestMapping("/register")
    @ResponseBody
    public Result register(@RequestParam("file") MultipartFile file, @RequestParam("file1") MultipartFile file1, ShopInfo info) throws IOException {
        ShopInfo info1 = infoMapper.selectByUser(info.getUserid());
        if (info1 != null) {
            return ResultUtils.err("该商家已入驻");
        }
        String path = UploadFilePathConfig.uploadPath;
        // 上传图片
        if (file != null && !file.isEmpty()) {
            // 获取文件的名称
            String oldname = file.getOriginalFilename();
            // 获取文件的后缀名
            String sux = oldname.substring(oldname.lastIndexOf("."));
            // 自动生成文件名
            String newname = UUID.randomUUID().toString() + sux;
            // 获取文件的上传路径（物理路径）
            String upload = path + newname;
            System.out.println(upload);
            // 上传图片
            file.transferTo(new File(upload));
            info.setIcon(newname);
        }
        if (file1 != null && !file1.isEmpty()) {
            // 获取文件的名称
            String oldname = file1.getOriginalFilename();
            // 获取文件的后缀名
            String sux = oldname.substring(oldname.lastIndexOf("."));
            // 自动生成文件名
            String newname = UUID.randomUUID().toString() + sux;
            // 获取文件的上传路径（物理路径）
            String upload = path + newname;
            System.out.println(upload);
            // 上传图片
            file1.transferTo(new File(upload));
            info.setPagecode(newname);
        }
        info.setState(0);
        return shopService.insertService(info);
    }

    @RequestMapping(value = "/update")
    @ResponseBody
    public Result update(Long id) {
        return shopService.updateShop(id);
    }

    @RequestMapping(value = "/selectByUser")
    @ResponseBody
    public Result selectByUser(Long id) {
        return shopService.selectByUser(id);
    }

    @RequestMapping(value = "/selectByState")
    @ResponseBody
    public Result selectByUser(int id) {
        return shopService.selectShopByState(id);
    }

}
